/**
 * jims
 */
package com.jims.api.v1.controller.sys;

import com.jims.master.common.data.Message;
import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.SecurityUtil;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.base.user.entity.MasterBaseinfo;
import com.jims.master.modules.base.user.entity.MasterDoctorinfo;
import com.jims.master.modules.base.user.service.MasterBaseinfoService;
import com.jims.master.modules.base.user.service.MasterDoctorinfoService;
import com.jims.master.modules.patient.myAttention.entity.PatFollow;
import com.jims.master.modules.patient.myFriend.entity.SysUserMessage;
import com.jims.master.modules.patient.myFriend.service.SysUserMessageService;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 发送消息Controller
 * @author WangYQ
 * @version 2017-07-13
 */
@Controller
@RequestMapping(value = "/user")
public class SysUserMessageController extends BaseController {

	@Autowired
	private SysUserMessageService sysUserMessageService;
	@Autowired
	private MasterBaseinfoService masterBaseinfoService;
	@Autowired
	private MasterDoctorinfoService masterDoctorinfoService;
	@Autowired
	private UserService userService;

	
	@ModelAttribute
	public SysUserMessage get(@RequestParam(required=false) String id) {
		SysUserMessage entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sysUserMessageService.get(id);
		}
		if (entity == null){
			entity = new SysUserMessage();
		}
		return entity;
	}
	/**
	 * @Title: saveMessageV100
	 * @Description: （保存聊天记录）
	 * @param faccid
	 * @param message
	 * @return java.lang.String
	 * @author WangYQ
	 * @date 2017-12-20
	 * @throws
	 */
	@RequestMapping(value = "V100/saveMessage",method ={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String saveMessage(String account, String faccid , String message) {
		Message msg=new Message();
	    StringData data=new StringData();
		int i=sysUserMessageService.save(account,faccid ,message);
        if(i>0){
            msg.setData("保存发送消息成功成功");
        }else{
            msg.setStatus("0");
            msg.setData("保存发送消息成功失败");
        }
        return SecurityUtil.encryptMsgData(msg);
	}
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(SysUserMessage sysUserMessage) {
	    StringData data=new StringData();
		int i=sysUserMessageService.delete(sysUserMessage);
        if(i>0){
            data.setCode("success");
            data.setData("删除发送消息成功成功");
        }else{
            data.setCode("error");
            data.setData("删除发送消息成功失败");
        }
		return data;
	}


	/**
	 * @Title:updateDoctor
	 * @Description: （沟通过的患者）
	 * @param baseId
	 * @return
	 * @author WangYQ
	 * @date 2018/07/16
	 * @throws
	 */
	@RequestMapping(value = "V100/communicationPerson",method ={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String communicationPerson(@RequestParam(required = true)String baseId,@RequestParam(required = true)String pageNo,String pageSize,HttpServletRequest request, HttpServletResponse response){
		Message mes = new Message();
		List result= new ArrayList<MasterBaseinfo>();
		//获取沟通过的患者
		MasterBaseinfo masterBaseinfo = masterBaseinfoService.get(baseId);
		User user=userService.findUserByPersonId(masterBaseinfo.getId());
		String id = user.getId();
		Page<SysUserMessage> page = new Page<SysUserMessage>();
		Map<String ,String> map = new HashMap<String, String>();
		map.put("pageNo",pageNo);
		map.put("pageSize",pageSize);
		map.put("id",id);
		page=sysUserMessageService.communicationPatient(new Page<SysUserMessage>(request, response), map);
		List<SysUserMessage> no = page.getList();
//				sysUserMessageService.communicationPatient(id);
		for(int j=0;j<no.size();j++){
			User user1 = userService.findUserByAccount(no.get(j).getFriendId());
			if (user1!=null){
				MasterBaseinfo ma1 = masterBaseinfoService.getInfo(user1.getPersionId());
				MasterDoctorinfo masterDoctorinfo=masterDoctorinfoService.getDoctorinfoByBaseinfoId(ma1.getId());
				if(masterDoctorinfo!=null){
					ma1.setMasterDoctorinfo(masterDoctorinfo);
				}
				ma1.setRemarks("0");
				result.add(ma1);
			}
		}
		mes.setData(result);
		return SecurityUtil.encryptMsgData(mes);
	}

}